import Storage from "good-storage";
import Config from "@/config/app";
import dayjs from "dayjs";

/**
 * 重置对象（会修改原始对象）
 * @param object
 * @param defaultVal
 */
export function resetObject(object, defaultVal = {}) {
  for (let k in object) {
    if (defaultVal.hasOwnProperty(k)) {
      object[k] = defaultVal[k];
    } else {
      if (Array.isArray(object[k])) object[k] = [];
      if ("string" == typeof object[k]) object[k] = "";
      if ("number" == typeof object[k]) object[k] = null;
      if ("boolean" == typeof object[k]) object[k] = false;
    }
  }
}

/**
 * 对象赋值（会修改原始对象）
 * @param object
 * @param valObject
 */
export function fillerLeft(object, valObject = {}) {
  for (let k in object) {
    if (valObject.hasOwnProperty(k)) {
      object[k] = valObject[k];
    }
  }
}

/**
 * 获取用户信息
 * @param key
 * @returns {null|*|undefined|{}}
 */
export function getUserInfo(key = null) {
  let userInfo = Storage.get(Config.userInfoKey);
  if (key) return userInfo.hasOwnProperty(key) ? userInfo[key] : null;
  return userInfo || {};
}

/**
 * 设置用户信息
 * @param user
 * @returns {*}
 */
export function setUserInfo(user) {
  Storage.set(Config.userInfoKey, user);
  return user;
}

/**
 * 设置权限
 * @param permissionsArr
 * @returns {*}
 */
export function setPermissions(permissionsArr) {
  Storage.set(Config.permissionsKey, permissionsArr);
  return permissionsArr;
}

/**
 * 获取权限
 * @returns {*|undefined|Array}
 */
export function getPermissions() {
  return Storage.get(Config.permissionsKey) || [];
}

/**
 * 判断是否有权限
 * @param permissionsKey
 * @returns {boolean}
 */
export function hasPermissions(permissionsKey) {
  let permissionsArr = Storage.get(Config.permissionsKey, []);
  return permissionsArr.indexOf(permissionsKey) !== -1;
}

/**
 * 获取Token
 * @returns {*|undefined}
 */
export function getToken() {
  return Storage.get(Config.tokenKey);
}

/**
 * 设置Token
 * @param token
 * @returns {*|undefined}
 */
export function setToken(token) {
  return Storage.set(Config.tokenKey, token);
}

/**
 * 移除Token
 * @returns {*}
 */
export function removeToken() {
  return Storage.remove(Config.tokenKey);
}

/**
 * @return {string}
 */
export function DateTimeParse(t, f = "YYYY-MM-DD") {
  let timestamp = parseInt(t.replace("/Date(", "").replace(")/", ""), 10);
  if (timestamp.toString().indexOf("-") !== -1) {
    return "";
  }
  let date = new Date(timestamp);
  return dayjs(date).format(f);
}

export function downloadFile(fileno) {
  if (fileno == undefined || fileno == "") {
    alert("不存在的文件编号！", 2);
    return false;
  }

  let form = document.createElement("form");
  form.style = "display:none";
  form.target = "callBackTarget";
  form.method = "get";
  form.action = "http://resource.hltadvisory.com/File/Download";

  let input1 = document.createElement("input");
  input1.type = "hidden";
  input1.name = "fileno";
  input1.value = fileno;
  form.appendChild(input1);
  document.body.appendChild(form);
  form.submit();
  document.body.removeChild(form);
}

/**
 * 将对象转换为?a=5&b=7形式
 * @param obj
 * @param firstStr
 * @returns {string|string}
 */
export function obj2StrParams(obj, firstStr = "?") {
  let params = firstStr;

  for (let p in obj) {
    params += p + "=" + obj[p] + "&";
  }
  return params.substring(0, params.length - 1);
}

export function openReport(params) {
  window.open(`/#/task_report${obj2StrParams(params)}`);
}
